﻿/**
 * 
 * TableView.as
 * @Ning
 * 2011-4-29
 * 
 * */

package ui.table_view
{
	import flash.display.DisplayObject;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.Point;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	
	import gs.TweenLite;
	
	import ui.scroll_view.ScrollView;
	
	public class TableView extends ScrollView
	{
		//START.

		static public const DIRECTION_HORIZONTAL:String = "direction_horizontal";
		static public const DIRECTION_VERTRICAL:String = "direction_vertrical";
		
		private var cells:Vector.<TableViewCell>;
		private var cacheCells:Vector.<TableViewCell>;
		
		private var contentChanged:Boolean;
		private var _direction:String;
		
		public var dataSource:I_TableViewDataSource;
		public var cacheSize:Number = 10;		
		
		
		//CONSTRUCTOR.
		public function TableView()
		{
			this.direction = DIRECTION_VERTRICAL;
		}

		// set direction.
		public function set direction(_direction:String):void
		{
			this._direction = _direction;
			lockDirectionH = direction == DIRECTION_VERTRICAL;
			lockDirectionV = direction == DIRECTION_HORIZONTAL;
		}
		
		// get direction.
		public function get direction():String
		{
			return _direction;
		}
		
		// load data.
		public function reloadData(offset:Point = null):void
		{
			if(dataSource == null) return;	
			
			cells = new Vector.<TableViewCell>;
			cacheCells = new Vector.<TableViewCell>;
			
			
			this.setContentSize(dataSource.getCellCount(this) * dataSource.getCellSize(this),
								dataSource.getCellCount(this) * dataSource.getCellSize(this));
			
			setContentOffset( offset == null ? new Point(0,0) : offset,false);

			this.pagingEnabled = true;
			this.pageSize = new Point(dataSource.getCellSize(this),dataSource.getCellSize(this));
			
			contentChanged = true;
			updatePosition(null);
		}
		
		
		// on update position.
		protected override function updatePosition(evt:Event):void
		{
			super.updatePosition(evt);

			if(!containerPositionChanged && !contentChanged)
				return;
			
			if(dataSource == null) 
				return;
			
			contentChanged = false;

			while(this.numContentChildren > 0)
				this.contentRemoveChildAt(0);
			
			var propPosition:String = direction == DIRECTION_HORIZONTAL ? "x" : "y";
			var propSize:String = direction == DIRECTION_HORIZONTAL ? "width" : "height";
			
			var topCellIndex:int = Math.floor(contentOffset[propPosition] / dataSource.getCellSize(this));
			var bottomCellIndex:int = Math.floor((contentOffset[propPosition] + this[propSize]) / dataSource.getCellSize(this));
			
			var tempCells:Vector.<TableViewCell> = new Vector.<TableViewCell>;
			
			for(var i:int = topCellIndex;i <= bottomCellIndex;i++)
			{
				if(i < 0 || i >= dataSource.getCellCount(this))
					continue;
				
				var cellView:DisplayObject = null;
				for each(var cell:TableViewCell in cells)
				{
					if(cell.cellIndex == i)
					{
						cellView = cell.cellContent;
						tempCells.push(cell);
						break;
					}
				}
				
				if(cellView == null)
				{
					for(var j:int = cacheCells.length - 1;j >= 0;j--)
					{
						var cacheCell:TableViewCell = cacheCells[j];
						if(cacheCell.cellIndex == i)
						{
							cellView = cacheCell.cellContent;
							tempCells.push(cacheCell);
							cacheCells.splice(j,1);
							break;
						}
					}					
				}
				
				if(cellView == null)
				{
					cellView = dataSource.cellByIndex(this,i);
					
					var cellModel:TableViewCell = new TableViewCell;
					cellModel.cellIndex = i;
					cellModel.cellContent = cellView;
					tempCells.push(cellModel);
				}
				cellView[propPosition] = i * dataSource.getCellSize(this); 
				this.contentAddChild(cellView);
			}
			
			// put cells in cache cell list.
			for each(var removeCell:TableViewCell in cells)
			{
				if(removeCell.cellIndex <  topCellIndex || 
					removeCell.cellIndex > bottomCellIndex)
				{
					cacheCells.push(removeCell);
					if(cacheCells.length > cacheSize)
						cacheCells.shift();
				}
			}
			
			cells = tempCells;
		}
		
		
		//END.
	}
}